<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use dh2y\login\login;
use think\Session;
use think\captcha\Captcha;
class Service extends Controller
{
	//登陆
	public function login()
	{
		$captcha= new captcha();
		$captcha->entry();
		$login = new login('userinfo');
		$data = request::instance()->post();
		$res=$login->doLogin($data);
		session::set('test','test');
		print_r(input('session.'));
		print_r($res);
	}
    //查询操作
    public function select()
    {
		$where='';
		if(null!=Request::instance()->get('field'))
		{
			$where[Request::instance()->get('field')]=Request::instance()->get('value');
		}
        $flag = Request::instance()->get('flag');
        $page = Request::instance()->get('page');
        $limit = Request::instance()->get('limit');
        $now = ($page - 1) * 10;
		$model=Model('admin/'.$flag);
		
		$data = $model->where($where)->limit($now,$limit)->select();
        //$data = Db::name($flag)->where($where)->limit($now, $limit)->select();
        $count = count($data);
        return json(0, "success", $count, $data);
    }
	//构造数据表格表头
    public function datagridfield($table)
    {
        $fields = Db::query('show full columns from cg_' . $table . ';');
        $string = "";
        foreach ($fields as $key => $val) {
            $string = $string . ",{field:'" . $val['Field'] . "', title: '" . $val['Comment'] . "'}";
        }
		
		$this->assign('tableframe',$fields);
        $this->assign('datagridfield', $string);
    }
	//构造查询修改表单
	
	public function rest($table)
	{
		$fields=Db::query("select column_name,column_comment,data_type from information_schema.columns where table_name='cg_".$table."'");
		$input  = Db::name('inputinfo')->select();
		foreach($fields as $k1 => $v1)
		{
			foreach($input as $k2=>$v2)
			{
				if($v1['data_type'] == $v2['type'])
				{
						$fields[$k1]['input']=$v2['input'];
				}
			}
		}
		return $fields;
	}
	public function add()
	{
		
		$data=Request::instance()->post();
		
		if(isset($data['data']['up_time']))
			$data['data']['up_time']=date("Y/m/d");
		Db::name($data['table'])->insert($data['data']);
		
		return "200";
	}
	public function edit()
	{
		
		$data=Request::instance()->post();
		if(isset($data['data']['up_time']))
			$data['data']['up_time']=date("Y/m/d");
		Db::name($data['table'])->update($data['data']);
		
		return "200";
	}
	public function del()
	{
		$data=Request::instance()->post();
		Db::name($data['table'])->delete($data['id']);
	}
}